﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using ImageLib;

namespace ImageDownloader
{
    /// <summary>
    /// Interaction logic for PluginWindow.xaml
    /// </summary> 

    public partial class PluginWindow : Window
    {
        private MainWindow parent;
        private ParserCollection parsers;
        private Parser parser;

        public PluginWindow(MainWindow parent) : base()
        {
            InitializeComponent();
            this.parent = parent;
        }

        public void SetParsers(ParserCollection parsers)
        {
            this.parsers = parsers;
            pluginList.ItemsSource = this.parsers;
        }

        public void SetCurrentParser(Parser parser)
        {
            this.parser = parser;
            pluginList.SelectedIndex = parsers.IndexOf(parser);
            Console.WriteLine("current = " + parsers.IndexOf(parser));
        }

        void pluginList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            parser = parsers[pluginList.SelectedIndex];
            Console.WriteLine("new  = " + parsers[pluginList.SelectedIndex] + " / " + pluginList.SelectedIndex);
        }

        private void removeButton_Click(object sender, RoutedEventArgs e)
        {
            if (parsers != null)
            {
                parsers.RemoveAt((sender as System.Windows.Controls.ListBox).SelectedIndex);
            }
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            if (!parent.TestPlugin(dllField.Text))
            {
                System.Windows.Forms.MessageBox.Show(MSG.errno4001, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            String newpath = parent.SavePlugin(dllField.Text);
            if (newpath.Equals(String.Empty))
            {
                System.Windows.Forms.MessageBox.Show(MSG.errno4002, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!parent.RegisterPlugin(newpath))
            {
                System.Windows.Forms.MessageBox.Show(MSG.errno4003, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
            FileDialog dialog = new OpenFileDialog();
            dialog.Filter = "DLL files (*.dll)|*.dll"; 
            dialog.ShowDialog();
            if (dialog.FileName != String.Empty)
            {
                dllField.Text = dialog.FileName;
            }
        }
    }
}
